#include "newexpensedialog.h"
#include "ui_newexpense.h"
#include "colorbutton.h"
#include "mainwindow.h"
#include <QDebug>

#define NUMBER_COLORS   16
#define COLOR_ITEM_1    0x00ff00
#define COLOR_ITEM_2    0x00ffff
#define COLOR_ITEM_3    0x0000ff
#define COLOR_ITEM_4    0x8781be
#define COLOR_ITEM_5    0xff00ff
#define COLOR_ITEM_6    0xf79779
#define COLOR_ITEM_7    0xfff200
#define COLOR_ITEM_8    0x00a651
#define COLOR_ITEM_9    0x00aef0
#define COLOR_ITEM_10   0xed008c
#define COLOR_ITEM_11   0x790000
#define COLOR_ITEM_12   0xa0410d
#define COLOR_ITEM_13   0xee145b
#define COLOR_ITEM_14   0xa3620a
#define COLOR_ITEM_15   0x00a651
#define COLOR_ITEM_16   0x00bff3

int color_arr[]={0x00ff00, 0x00ffff, 0x0000ff, 0x8781be,
             0xff00ff, 0xf79779, 0xfff200, 0x00a651,
             0x00aef0, 0xed008c, 0x790000, 0xa0410d,
             0xee145b, 0xa3620a, 0x00a651, 0x00bff3
            };
QString convertColor(int color);
NewExpenseDialog::NewExpenseDialog(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::NewExpense)
{
    ui->setupUi(this);
    this->Initialize();
}

NewExpenseDialog::~NewExpenseDialog()
{
    delete ui;
}

void NewExpenseDialog::Initialize()
{
    //database.openConnection();
    this->color = "00ff00";
    ColorButton *color_btn;
    int i;
    int x = 0;
    int y = 0;
    QString obj_name;
    QString color_str;
    QButtonGroup* btn_group = new QButtonGroup(this);

    this->ui->pushButton_final->setStyleSheet("background-color: #00ff00; color: rgb(255, 255, 255)");

    for (i = 0; i < NUMBER_COLORS; i++){
        y = (i / 8) * 70;
        x = (i % 8) * 70;
        color_str = convertColor(i);
        obj_name = "color_btn_" + QString::number(i);
        color_btn = new ColorButton(this);
        color_btn->setObjectName(obj_name);
        color_btn->setGeometry(QRect(x, y, 64, 64));
        color_btn->setAutoFillBackground(true);
        color_btn->setStyleSheet("background-color: #" + color_str + "; color: rgb(255, 255, 255)");
        color_btn->setColor(color_arr[i]);
        btn_group->addButton(color_btn, i);

    }
    btn_group->setExclusive(true);
    QObject::connect(btn_group, SIGNAL(buttonClicked(int)), this, SLOT(on_colorButtonGroup_clicked(int)));

}

void NewExpenseDialog::on_buttonBox_rejected()
{
    this->close();
}

void NewExpenseDialog::on_buttonBox_accepted()
{
    ExpenseItems *newItem =  new ExpenseItems();
    int id;
    newItem->setColor(this->color);
    newItem->setName(this->ui->lineEditExpenseName->text());
    id = this->database.updateExpenseItems(newItem);
    ((AddExpendDialog*)this->parent())->loadExpenseItemsList();

//    QString obj_name = "color_btn_" + QString::number(id);
//    ColorButton *color_btn = new ColorButton(this);//((AddExpendDialog*)this->parent());
//    color_btn->setObjectName(obj_name);
//    color_btn->setGeometry(QRect(0, 140, 64, 64));
//    color_btn->setAutoFillBackground(true);
//    qDebug() << "add exp:" << this->color;
//    color_btn->setStyleSheet("background-color: #" + this->color + "; color: rgb(255, 255, 255)");
//    color_btn->show();
//    ((AddExpendDialog*)this->parent())->getBtn_group()->addButton(color_btn, id);


    this->close();
}

void NewExpenseDialog::on_colorButtonGroup_clicked(int id)
{
    QString color_str;
    color_str = convertColor(id);
    qDebug() << "on_colorButtonGroup_clicked color_str=" << color_str;

    this->color = color_str;
    this->ui->pushButton_final->setAutoFillBackground(true);

    this->ui->pushButton_final->setStyleSheet("background-color: #" + color_str + "; color: rgb(255, 255, 255)");
}

QString convertColor(int color){
    QString color_str;

    color_str =  QString::number(color_arr[color], 16);
    switch (color_str.length()){
        case 1:
            color_str="00000" + color_str;
        break;
        case 2:
            color_str="0000" + color_str;
        break;
        case 3:
            color_str="000" + color_str;
        break;
        case 4:
            color_str="00" + color_str;
        break;
        case 5:
            color_str="0" + color_str;
        break;
    }

    return color_str;
}

